/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package itproject.clients.gui;

import itproject.data.Customer;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;

/**
 *
 * @author tim
 */
public class EBInterface implements ActionListener, TreeSelectionListener {

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }
    private JMenuBar mainMenuBar;
    private JMenu fileJMenu;
    private JMenuItem exit;
    private JFrame clientMainFrame;
    private JPanel contentPanel;
    private JPanel mainPanel;
    private JSplitPane splitPane;
    private JPanel buttonPanel;
    private JScrollPane scrollPane;
    EBInterfaceClient ebic = new EBInterfaceClient();
    private EBMenuTree easyBookingMenuTree;
    private CustomerForm cf = new CustomerForm(this);
    private CreateCustomerDialog ccd = new CreateCustomerDialog(this);
    private EditCustomerDialog ecd = new EditCustomerDialog(this);

    public EBInterface() {
        //new EasyBookingInterfaceConfig(this);
        //this.createAndShowGUI();
    }

    public static void createAndShowGUI() {
        EBInterface ebi = new EBInterface();
        ebi.createFrame();



    }

    public void createFrame() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }

        clientMainFrame = new JFrame("EasyBooking Client");
        clientMainFrame.setDefaultLookAndFeelDecorated(true);
        clientMainFrame.setPreferredSize(new Dimension(1000, 800));
        clientMainFrame.setJMenuBar(this.createMenuBar());
        clientMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clientMainFrame.getContentPane().add(createSplitPane());
        clientMainFrame.setResizable(true);
        clientMainFrame.setLocationRelativeTo(null);
        clientMainFrame.pack();
        clientMainFrame.setVisible(true);



    }
    // Creates the Menu Bar

    private JMenuBar createMenuBar() {

        mainMenuBar = new JMenuBar();
        fileJMenu = new JMenu("File");
        exit = new JMenuItem("Exit");

        exit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        fileJMenu.add(exit);
        mainMenuBar.add(fileJMenu);
        return mainMenuBar;
    }

    private JSplitPane createSplitPane() {
        // creates the split pane and adds the tree manu and the main panel
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setLeftComponent(this.createTreeMenu());
        splitPane.setRightComponent(this.createStartPanel());
        return splitPane;
    }

    public void setRightPanel(JPanel p) {
        splitPane.setRightComponent(p);
        splitPane.repaint();
        clientMainFrame.getContentPane().repaint();
        clientMainFrame.repaint();
    }

    // Creates the scroll pane and adds the menu tree
    private JScrollPane createTreeMenu() {

        // Gets the singelton easyBookingMenuTree object
        easyBookingMenuTree = EBMenuTree.EasyBookingMenuTree(this);

        // Expand all nodes
        easyBookingMenuTree.expandAll();

        // Creates the
        scrollPane = new JScrollPane(easyBookingMenuTree);
        return scrollPane;
    }

    // Creates the ConfigPanel Form
    private JPanel createStartPanel() {
        mainPanel = new JPanel();

        // Sets the border
        mainPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Easybooking Client "),
                BorderFactory.createEmptyBorder(20, 20, 20, 20)));

        // Sets the layout of the mainPanel
        mainPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        // Places the GUI elements in the layout using addComponent                                           x, y, w, h, wx, wy       
        JLabel lblServerHost = new JLabel("StartPanel:");
        addComponent(mainPanel, gbl, lblServerHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);

        JTextField txfServerHost = new JTextField(20);
        addComponent(mainPanel, gbl, txfServerHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);

        return mainPanel;
    }

    // Creates the button panel
    public JPanel createButtonPanel() {
        buttonPanel = new JPanel();


        // Sets the buttons the same size
        //btnOk.setPreferredSize(btnCancel.getPreferredSize());

        // Registrate action listeners


        return buttonPanel;
    }

    // Adds the elements to the layout
    static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("connectToServer")) {
            // TODO: Connect to server

            this.createAndShowGUI();
            return;
        }
        if (e.getActionCommand().equals("searchCustomer")) {
            JOptionPane.showMessageDialog(null, cf.getSearchText());
        }
        if (e.getActionCommand().equals("showCreateCustomerDialog")) {


            ccd.createAndShowDialog();
        }

        if (e.getActionCommand().equals("createCustomerDialogOk")) {

            // validate input of the zip field
            try {

                int x = Integer.parseInt(ccd.getZip());
            } catch (NumberFormatException nFE) {
                JOptionPane.showMessageDialog(null, "Please enter a correct Zip-Code");
            }

            // adds a new customer example data
            ebic.createCustomer(ccd.getFirstName(), ccd.getLastName(), ccd.getStreet(), ccd.getZip(), ccd.getCity());

            // gets and sets the whole table data
            cf.setTableData(ebic.getAllCustomers());

            // cleans the view
            cf.removeAll();

            // renews the cf
            cf.createAndShowForm();

            // shows the cf again
            this.setRightPanel(cf);

            // closes the ccd
            ccd.dispose();
        }
        if (e.getActionCommand().equals("editCustomer")) {
            if (cf.getSelectedCustomer() == null) {
                JOptionPane.showMessageDialog(null, "No customer selected");
            } else {
                Customer c = (Customer) cf.getSelectedCustomer();
                JOptionPane.showMessageDialog(null, "Editing: " + c.getFirstname() + " " + c.getLastname());

                // pases the customer which should be edited
                ecd.createAndShowDialog(c);
            }
        }

        if (e.getActionCommand().equals("editCustomerDialogOk")) {

            // validate input of the zip field
            try {

                int x = Integer.parseInt(ecd.getZip());
            } catch (NumberFormatException nFE) {
                JOptionPane.showMessageDialog(null, "Please enter a correct Zip-Code");
            }

            // adds a new customer example data
            ebic.updateCustomer(ecd.getFirstName(), ecd.getLastName(), ecd.getStreet(), ecd.getZip(), ecd.getCity(), ecd.getEditableCustomer());


            // gets and sets the whole table data
            cf.setTableData(ebic.getAllCustomers());

            // cleans the view
            cf.removeAll();

            // renews the cf
            cf.createAndShowForm();

            // shows the cf again
            this.setRightPanel(cf);

            // closes the ccd
            ecd.dispose();
        }

        if (e.getActionCommand().equals("deleteCustomer")) {
            if (cf.getSelectedCustomer() == null) {
                JOptionPane.showMessageDialog(null, "No customer selected");
            } else {
                Customer c = (Customer) cf.getSelectedCustomer();
                JOptionPane.showMessageDialog(null, "Deleting: " + c.getFirstname() + " " + c.getLastname());
                ebic.deleteCustomer(c);


                cf.setTableData(ebic.getAllCustomers());

                // cleans the view
                cf.removeAll();

                // renews the cf
                cf.createAndShowForm();

                // shows the cf again
                this.setRightPanel(cf);

                // closes the ccd
                ccd.dispose();
            }
        }
    }

    public void valueChanged(TreeSelectionEvent e) {
        TreePath path = e.getNewLeadSelectionPath();

        Object a = path.getLastPathComponent().toString();

        if (a == "Customer") {

            // removes all old components
            cf.removeAll();

            // set table date
            cf.setTableData(ebic.getAllCustomers());

            // prepare form for displaying
            cf.createAndShowForm();

            // show cf in the right panel
            this.setRightPanel(cf);
        }
        if (a == "EasyBooking") {
            this.setRightPanel(mainPanel);
        }
    }
}
